//
//  UserInfoPageController.m
//  ELife
//
//  Created by feng lu on 11-6-7.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "UserInfoPageController.h"


@implementation UserInfoPageController

@synthesize name, account, score, daijinquan, myPhoto;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)init:(id)target
{
    if ([self init]) {
        
    }
    delegate = target;
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self openMyImage];
    //UIImagePickerController
    // Do any additional setup after loading the view from its nib.
    
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentPaths objectAtIndex:0];
    NSLog(@"%@",documentDir);
    
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    
    NSString *imageFile = [documentDir stringByAppendingPathComponent:@"temp.jpg"];
    BOOL find = [fileManager fileExistsAtPath:imageFile];
    
    NSLog(@"f=%i", find);
    
    [myPhoto setImage:[UIImage imageWithContentsOfFile:imageFile] forState:UIControlStateNormal];
}

-(void) openMyImage
{
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentPaths objectAtIndex:0];
    NSLog(@"%@",documentDir);
    
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    
    NSString *imageFile = [documentDir stringByAppendingPathComponent:@"temp.jpg"];
    BOOL find = [fileManager fileExistsAtPath:imageFile];
    
    NSLog(@"f=%i", find);
    
    [myPhoto setImage:[UIImage imageWithContentsOfFile:imageFile] forState:UIControlStateNormal];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction) openMenu
{
    
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
	picker.delegate = self;
    
	picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
	[self presentModalViewController:picker animated:YES];
}


- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
	[picker dismissModalViewControllerAnimated:YES];
    UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    image = [image scaleWithSize:CGSizeMake(103, 103)];
    [myPhoto setImage:image forState:UIControlStateNormal];
    
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentPaths objectAtIndex:0];
    NSLog(@"%@",documentDir);
    
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    
    NSString *imageFile = [documentDir stringByAppendingPathComponent:@"temp.jpg"];
    NSLog(@"%@", imageFile);
    
    success = [fileManager fileExistsAtPath:imageFile];
    if(success) {
        success = [fileManager removeItemAtPath:imageFile error:&error];
    }
    
    [UIImageJPEGRepresentation(image, 1.0f) writeToFile:imageFile atomically:YES];
    
    
    [delegate openMyImage];
    //SETIMAGE(image);
    //CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
    
    //NSData* myData = UIImageJPEGRepresentation(image, 0);
    
	//NSDocument
}
@end
